﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EnterpriseLibraryWrapper;

namespace itau.im.dado
{
    public static class CalendarioSingleton
    {
        private static List<DateTime> lista = new List<DateTime>();

        public static bool HouveConsulta()
        {
            return lista.Count > 0;
        }

        public static void Configurar(int diasExtensao)
        {
            CalendarioBS.DatasServiceSoapClient cal = new CalendarioBS.DatasServiceSoapClient();

            try
            {
                DateTime dataInicio = System.DateTime.Now.AddYears(-15);
                //DateTime dataFim = System.DateTime.Now.AddDays(diasExtensao);
                DateTime dataFim = System.DateTime.Now.AddYears(diasExtensao);

                string DiaDataInicial = dataInicio.Day.ToString();
                string MesDataInicial = dataInicio.Month.ToString();
                string AnoDataInicial = dataInicio.Year.ToString();

                string DiaDataFinal = dataFim.Day.ToString();
                string MesDataFinal = dataFim.Month.ToString();
                string AnoDataFinal = dataFim.Year.ToString();

                CalendarioBS.InfoFeriadosResponse feriadosResponse = cal.ObtemFeriadosNacionaisIntervalo(DiaDataInicial,
                                                                 MesDataInicial,
                                                                 AnoDataInicial,
                                                                 DiaDataFinal,
                                                                 MesDataFinal,
                                                                 AnoDataFinal);
                lista.Clear();

                foreach (Object item in feriadosResponse.EstadosInfo)
                {
                    string strDate = ((CalendarioBS.FeriadoInfoClass)item).Data_Feriado;
                    System.Globalization.DateTimeFormatInfo dtfi = new System.Globalization.DateTimeFormatInfo();
                    dtfi.ShortDatePattern = "dd/MM/yyyy";
                    dtfi.DateSeparator = "/";
                    DateTime objDate = Convert.ToDateTime(strDate, dtfi);
                    lista.Add(objDate);
                }
            }
            catch (DadosException ex)
            {
                EnterpriseLibraryFactory.GetExceptionHandler().HandleException(ex, "ErrosDados");
            }
            catch (Exception ex)
            {
                EnterpriseLibraryFactory.GetExceptionHandler().HandleException(ex, "ErrosDados");
            }
            finally
            {
                cal.Abort();
                cal.Close();
            }
        }

        public static List<string> ObtemFeriadosNacionaisIntervalo(DateTime dataInicio, DateTime dataFim)
        {
            return (from data in lista
                    where data >= dataInicio && data <= dataFim
                    select data.ToString("yyyyMMdd")).ToList<string>();
        }
    }
}
